import { fetchApiWithGet, fetchApiWithPost } from '../utils/index'
import { 
	FETCH_ADDRESS_SUCC,
	FETCH_ADDRESS_FAIL,
	ADD_ADDRESS_SUCC,
	ADD_ADDRESS_FAIL,
	DEL_ADDRESS_SUCC,
	DEL_ADDRESS_FAIL,
	EDIT_ADDRESS_SUCC,
	EDIT_ADDRESS_FAIL,
	SET_DEFAULT_ADD_SUCC,
	SET_DEFAULT_ADD_FAIL, 
} from '../contants/address'

const publicUrl = 'http://fengke.net:8081/shop/';

export function fetchAddress() {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}address_show`;
	var argus = {
		userId: userId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, FETCH_ADDRESS_SUCC, FETCH_ADDRESS_FAIL)
	}
}

export function addAddress(name, phone, p_name, c_name, d_name, content, postCode) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}address_add`;
	var argus = {
		userId: userId,
		name: name,
		phone: phone,
		p_name: p_name,
		c_name: c_name,
		d_name: d_name,
 		content: content,
		postCode: postCode
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, ADD_ADDRESS_SUCC, ADD_ADDRESS_FAIL)
	}
}

export function delAddress(addressId) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}address_del`;
	var argus = {
		userId: userId,
		addressId: addressId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, DEL_ADDRESS_SUCC, DEL_ADDRESS_FAIL)
	}
}

export function editAddress(addressId, name, phone, p_name, c_name, d_name, content, postCode) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}address_mod`;
	var argus = {
		userId: userId,
		addressId: addressId,
		name: name,
		phone: phone,
		p_name: p_name,
		c_name: c_name,
		d_name: d_name,
		content: content,
		postCode: postCode
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, EDIT_ADDRESS_SUCC, EDIT_ADDRESS_FAIL)
	}
}

export function setDefaultAddress(addressId) {
	var userId = localStorage.getItem('session');
	var url = `${publicUrl}address_setDef`;
	var argus = {
		userId: userId,
		addressId: addressId
	};
	return dispatch => {
		fetchApiWithPost(url, argus, dispatch, SET_DEFAULT_ADD_SUCC, SET_DEFAULT_ADD_FAIL)
	}
}

